package com.colin.aiyi.ui.saySearch;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.fragment.SaySearchFragment;
import com.colin.aiyi.ui.BaseActivity;
import com.colin.aiyi.utils.MusicUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hl on 2017/6/2.
 */

public class SayListAc extends BaseActivity {

    @BindView(R.id.frame_layout)
    FrameLayout frameLayout;
    @BindView(R.id.text_banner_left)
    TextView textBannerLeft;
    @BindView(R.id.text_banner_title)
    TextView textBannerTitle;
    @BindView(R.id.text_banner_rigth)
    TextView textBannerRigth;
    @BindView(R.id.img_totop)
    ImageView imgTotop;
    @BindView(R.id.img_small)
    ImageView imgSmall;
    @BindView(R.id.text_music_title)
    TextView textMusicTitle;
    @BindView(R.id.seekbar)
    SeekBar seekbar;
    @BindView(R.id.text_music_time)
    TextView textMusicTime;
    @BindView(R.id.img_play)
    ImageView imgPlay;
    @BindView(R.id.img_play_audio)
    ImageView imgPlayAudio;
    @BindView(R.id.text_music_time_sum)
    TextView textMusicTimeSum;
    @BindView(R.id.line_buttom)
    LinearLayout lineButtom;
    private int contentType = 1;  //1:人气 2：最新 ,已购买3
    private String content = "";
    private View itemMusic;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_say_list);
        ButterKnife.bind(this);
        if (getIntent() != null) {
            contentType = getIntent().getIntExtra("contentType", 1);
            content = getIntent().getStringExtra("searchContent");
        }
        textBannerTitle.setText("导师说搜索");
        textBannerLeft.setOnClickListener(this);
        itemMusic = findViewById(R.id.item_music);
        initView();
        showFButton(Constant.SHOW_NONE);
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                MusicUtil.getInstance().pause(seekBar.getProgress());
            }
        });

    }

    private void initView() {
        SaySearchFragment fragment = new SaySearchFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("contentType", contentType);
        bundle.putString("searchContent", content);
        fragment.setArguments(bundle);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_layout, fragment).commit();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateMusicEvent(MusicUtil.MuiscEvent muiscEvent) {
        switch (muiscEvent.getEventType()) {
            case MusicUtil.MEDIA_STATUS_START:
                Glide.with(this).load(MusicUtil.getInstance().getMusicPic()).into(imgSmall);
                textMusicTitle.setText("正在缓冲中......");
                textMusicTime.setText("0:00");
                textMusicTimeSum.setText("0:00");
                seekbar.setMax(100);
                seekbar.setProgress(0);
                imgPlay.getBackground().setLevel(0);
                showFButton(Constant.SHOW_MUSIC_ONLY);
                break;
            case MusicUtil.MEDIA_STATUS_PREPARE:
            case MusicUtil.MEDIA_STATUS_PLAY:
                showFButton(Constant.SHOW_MUSIC_ONLY);
                if (!MusicUtil.getInstance().isAutoPlay()) {
                    MusicUtil.getInstance().play();
                }
                Glide.with(this).load(MusicUtil.getInstance().getMusicPic()).into(imgSmall);
                textMusicTitle.setText(MusicUtil.getInstance().getMusicTitle());
                textMusicTime.setText(MusicUtil.getMusicTime(muiscEvent.getMediaPlayer().getCurrentPosition()));
                textMusicTimeSum.setText(MusicUtil.getMusicTime(muiscEvent.getMediaPlayer().getDuration()));
                seekbar.setMax(muiscEvent.getMediaPlayer().getDuration());
                seekbar.setProgress(muiscEvent.getMediaPlayer().getCurrentPosition());
                imgPlay.getBackground().setLevel(1);
                break;
            case MusicUtil.MEDIA_STATUS_PAUSE:
                imgPlay.getBackground().setLevel(0);
                showFButton(Constant.SHOW_MUSIC_ONLY);
                break;
            case MusicUtil.MEDIA_STATUS_STOP:
                showFButton(Constant.HIDE_MUSIC);
                imgPlay.getBackground().setLevel(0);
            case MusicUtil.MEDIA_STATUS_COMLETION:
                showFButton(Constant.HIDE_MUSIC);
                imgPlay.getBackground().setLevel(0);
                break;
        }
    }

    @Override
    public void showFButton(int level) {
        switch (level) {
            case Constant.SHOW_NONE:
                lineButtom.setVisibility(View.GONE);
                MusicUtil.getInstance().stop();
                break;
            case Constant.SHOW_GOTOP_ONLY:
                lineButtom.setVisibility(View.VISIBLE);
                imgTotop.setVisibility(View.VISIBLE);
                itemMusic.setVisibility(View.GONE);
                MusicUtil.getInstance().stop();
                break;
            case Constant.SHOW_MUSIC_ONLY:
                lineButtom.setVisibility(View.VISIBLE);
                imgTotop.setVisibility(View.GONE);
                itemMusic.setVisibility(View.VISIBLE);
                break;
            case Constant.SHOW_MUSIC:
                lineButtom.setVisibility(View.VISIBLE);
                itemMusic.setVisibility(View.VISIBLE);
                break;
            case Constant.HIDE_MUSIC:
                itemMusic.setVisibility(View.GONE);
                break;
            case Constant.SHOW_ALL:
                lineButtom.setVisibility(View.VISIBLE);
                imgTotop.setVisibility(View.VISIBLE);
                itemMusic.setVisibility(View.VISIBLE);
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.text_banner_left:
                onBackPressed();
                break;
            case R.id.img_play:
                MusicUtil.getInstance().play();
                break;
            case R.id.img_play_audio:
                MusicUtil.getInstance().stop();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (MusicUtil.getInstance().isPlaying())
        {
            MusicUtil.getInstance().stop();
        }
        finish();
    }


}
